package com.varma.samples.smsdemo;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.telephony.SmsMessage;
import android.util.Log;
import android.widget.Toast;

public class DeliverReceiver extends BroadcastReceiver
{
	@Override
	public void onReceive(Context context, Intent intent)
	{
		Log.d("cgm", "receive new sms");
		SharedPreferences sharedPreferences =context.getSharedPreferences("cgm", Context.MODE_PRIVATE);
		boolean select = sharedPreferences.getBoolean("select", false);
		if (!select){
			Log.d("cgm", "not select for black list");
			return;
		}
		SmsMessage[] msgs = null;
		Bundle bundle = intent.getExtras();     
		String info = "Binary SMS from ";
        Object[] pdus = (Object[]) bundle.get("pdus");
        msgs = new SmsMessage[pdus.length];
        byte[] data = null;
        boolean blackList = false;
        for (int i=0; i<msgs.length; i++){
            msgs[i] = SmsMessage.createFromPdu((byte[])pdus[i]);                
            String address = msgs[i].getOriginatingAddress();                     
            Log.d("cgm","get from address:"+msgs[i].getOriginatingAddress());
            
            if (address.equals("15306984177")){
            	 Log.d("cgm","in black list");
            	blackList = true;
            	break;
            }
        }
        
        if (blackList){
        	setResultCode(-101);
        }
	}

}
